const WebpackAliyunOss = require('webpack-aliyun-oss')

let webpackAliyunOss = [
  new WebpackAliyunOss({
    from: ['./dist/**', '!./dist/**/*.html'],
    dist: 'web',
    region: 'oss-us-west-1',
    accessKeyId: process.env.accessKeyId,
    accessKeySecret: process.env.accessKeySecret,
    bucket: 'bonoch-test',
    setOssPath: filePath => {
      let index = filePath.lastIndexOf("dist");
      let Path = filePath.substring(index + 4, filePath.length);
      return Path.replace(/\\/g, "/");
    },
    setHeaders: filePath => {
      // console.log(filePath)
      return {
        "Cache-Control": "max-age=31536000"
      }
    }
  })
]

module.exports = {
  outputDir: 'dist',//打包输出文件夹
  assetsDir: 'static',
  lintOnSave: false,
  devServer:{
    port: 8085,
    proxy: 'http://admin.bonoch.top',
    host:'admin.bonoch.top',
  },
  productionSourceMap: false, // 生产环境是否生成 sourceMap 文件
  chainWebpack: config => {
    /**
     * 删除懒加载模块的prefetch，降低带宽压力
     * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch
     * 而且预渲染时生成的prefetch标签是modern版本的，低版本浏览器是不需要的
     */
    config.plugins.delete('preload')
    config.plugins.delete('prefetch')

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config.sideEffects = true
          config.concatenateModules = true
          config.minimize = true
          config.optimization.splitChunks({
              chunks: 'all',
              maxInitialRequests: 5,
              minSize: 0,
              cacheGroups: {
                common: {
                  name: 'chunk-common',
                  chunks: "initial",
                  priority: 1,
                  minChunks: 2,
                  reuseExistingChunk: true,
                  enforce: true
                },
                vendors: {
                  name: "chunk-vendors",
                  test: /[\\/]node_modules[\\/]/,
                  chunks: "all",
                  priority: 2,
                  reuseExistingChunk: true,
                  enforce: true
                },
                vue: {
                  name: "chunk-vue",
                  test: /[\\/]node_modules[\\/]vue[\\/]/,
                  chunks: "all",
                  priority: 3,
                  reuseExistingChunk: true,
                  enforce: true
                },
                elementUI: {
                  name: "chunk-elementui",
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
                  chunks: "all",
                  priority: 4,
                  reuseExistingChunk: true,
                  enforce: true
                },
                vueRouter: {
                  name: "chunk-vue-router",
                  test: /[\\/]node_modules[\\/]_?vue-router[\\/]/,
                  chunks: "all",
                  priority: 5,
                  reuseExistingChunk: true,
                  enforce: true
                },
                vuex: {
                  name: "chunk-vuex",
                  test: /[\\/]node_modules[\\/]vuex[\\/]/,
                  chunks: "all",
                  priority: 6,
                  reuseExistingChunk: true,
                  enforce: true
                },
                vueI18n: {
                  name: "chunk-vuei18n",
                  test: /[\\/]node_modules[\\/]vue-i18n[\\/]/,
                  chunks: "all",
                  priority: 7,
                  reuseExistingChunk: true,
                  enforce: true
                },
                axios: {
                  name: "chunk-axios",
                  test: /[\\/]node_modules[\\/]_?axios(.*)/,
                  chunks: "all",
                  priority: 8,
                  reuseExistingChunk: true,
                  enforce: true
                }
              }
            })
          }
      )
  },
  configureWebpack: (config) => {
    if(process.env.OSS === '1') {
      config.plugins.push(webpackAliyunOss[0])
    }
  },
}
